package com.juvepoland.jpdroid.activities;

import java.util.List;

import com.juvepoland.jpdroid.R;
import com.juvepoland.jpdroid.adapters.LazyNewsAdapter;
import com.juvepoland.jpdroid.managers.NewsManager;
import com.juvepoland.jpdroid.managers.impl.NewsManagerImpl;
import com.juvepoland.jpdroid.models.News;
import com.juvepoland.jpdroid.models.NewsResultType;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

abstract public class BasicTabbedActivity extends BasicActivity {

	protected ListView list;
	protected LazyNewsAdapter adapter;
	protected NewsManager newsManager;
	protected TextView idTextView;
	protected int newsIndex = 0; 
	protected int newsCountPerSinglePage;	
	protected int maxPageLoadClickedCount;	
	protected Button btnLoadMore;
	//protected Button btnLoadPrev;	
	protected NewsResultType newsResultType;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.all_news_layout);
		this.newsResultType = getNewsResultType();
		list=(ListView)findViewById(R.id.list);
		newsManager = new NewsManagerImpl();
		
		
		btnLoadMore = new Button(getApplicationContext());
		btnLoadMore.setTypeface(basicFontTypeFace);
		
		//btnLoadPrev = new Button(getApplicationContext());
		//btnLoadPrev.setTypeface(basicFontTypeFace);
        
		btnLoadMore.setText("więcej >>>");	//TODO pobrać z pliku
		//btnLoadPrev.setText("<<< wstecz");
		
		
		list.addFooterView(btnLoadMore);
		//list.addHeaderView(btnLoadPrev);
		
		newsCountPerSinglePage = sharedPreferences.getInt(MyPreferencesActivity.PREFERENCE_NAME_NEWS_COUNT_PER_SINGLE_PAGE, 
				Integer.parseInt(properties.getProperty(MyPreferencesActivity.PREFERENCE_NAME_NEWS_COUNT_PER_SINGLE_PAGE)));	
		maxPageLoadClickedCount = sharedPreferences.getInt(MyPreferencesActivity.PREFERENCE_NAME_MAX_PAGE_LOAD_CLICKED_COUNT, 
				Integer.parseInt(properties.getProperty(MyPreferencesActivity.PREFERENCE_NAME_MAX_PAGE_LOAD_CLICKED_COUNT)));	
		
		
		new AsyncActionClass().execute(null);
		
		addButtonLoadMoreOnClickListener();
		
		//addButtonLoadPrevOnClickListener();
		
		addListOnClickListener();
		
		
	}
	
	
	


	
	protected void addListOnClickListener() {
		
		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				TextView idTextView =  (TextView) arg1.findViewById(R.id.pid);
				Intent singleNewsIntent = new Intent(getApplicationContext(), SingleNewsActivity.class);
				singleNewsIntent.putExtra("news_id",  Integer.parseInt(idTextView.getText().toString()));
				startActivity(singleNewsIntent);
			}
		});
		
	}


	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
	 super.onRestoreInstanceState(savedInstanceState);
	 newsIndex = savedInstanceState.getInt("newsIndex", 0);
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
	 super.onSaveInstanceState(outState);
	 outState.putInt("newsIndex", newsIndex);
	}
	


	protected void addButtonLoadMoreOnClickListener() {
		
		btnLoadMore.setOnClickListener(new View.OnClickListener() {
		    @Override
		    public void onClick(View arg0) {
		    		newsIndex = newsIndex+newsCountPerSinglePage;
			        new AsyncActionClass().execute(null);
		    }
		});
	}
	
	/*private void addButtonLoadPrevOnClickListener() {
		btnLoadPrev.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
					newsIndex = newsIndex-newsCountPerSinglePage;
					new AsyncActionClass().execute(null);
			}
		});
		
	}*/


	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.layout.news_activity_menu, menu);
		return true;
	}
	
	
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
		if(item.getItemId() == R.id.refresh_bookmark){
				new AsyncActionClass().execute(null);
				return true;
		}
		else{
			 return super.onOptionsItemSelected(item);
		}
	}


	
	abstract NewsResultType getNewsResultType();

	@Override
	protected Intent getIntentToReturnAfterLogout() {
		return new Intent(getApplicationContext(), NewsActivity.class);
	}



	@Override
	protected void doInBackgroundAsyncProcess() throws Exception{
		
		boolean displayImages = sharedPreferences.getBoolean(MyPreferencesActivity.PREFERENCE_NAME_DISPLAY_NEWS_IMAGES, Boolean.parseBoolean(properties.getProperty(MyPreferencesActivity.PREFERENCE_NAME_DISPLAY_NEWS_IMAGES)));
		
		List<News> actualLoadedNewsList = newsManager.getNewsList(newsResultType, newsIndex, newsCountPerSinglePage);
	
		adapter = new LazyNewsAdapter(BasicTabbedActivity.this,
				actualLoadedNewsList, basicFontTypeFace, basicItalicFontTypeFace, displayImages); // !
		

		list.setAdapter(adapter);
		
		list.setSelectionFromTop(newsIndex, 0);
		 
		if(newsIndex >=maxPageLoadClickedCount*newsCountPerSinglePage
				||
			actualLoadedNewsList.size() < newsCountPerSinglePage){
			btnLoadMore.setVisibility(LinearLayout.GONE);
		}
		else{
			btnLoadMore.setVisibility(LinearLayout.VISIBLE);
		}
		
	}


	
}
